#include <QThreadPool>
#include "oxford-custom-list.h"
#include "oxford-word-provider.h"
#include "oxford-word-updater.h"

using namespace Oxford;

CustomList::CustomList(const QString& listName, const QString& listDescription, const QStringList& words)
{
    _listName = listName;
    _listDescription  = listDescription;
    databaseManager = new Core::DatabaseManager("Oxford");
    update(words);
}

CustomList::~CustomList()
{
    delete databaseManager;
}

QString Oxford::CustomList::name() const
{
    return _listName;
}

QString Oxford::CustomList::description() const
{
    return _listDescription;
}

QString Oxford::CustomList::getRandomWord()
{
    return databaseManager->getRandomListItem(_listName);
}

QStringList Oxford::CustomList::getList()
{
    return databaseManager->getList(_listName);
}

void Oxford::CustomList::update(const QStringList &words)
{
    if (!words.isEmpty())
        databaseManager->storeList(_listName, words);

    WordUpdater *updater = new WordUpdater();
    updater->setAutoDelete(true);
    updater->setDeepMode(false);
    updater->setWordsList(getList());

    QThreadPool::globalInstance()->start(updater);
}
